package com.louzhu.gulimall.product.service.impl;

import com.louzhu.gulimall.product.entity.SkuImagesEntity;
import com.louzhu.gulimall.product.entity.SpuInfoDescEntity;
import com.louzhu.gulimall.product.service.*;
import com.louzhu.gulimall.product.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.louzhu.common.utils.PageUtils;
import com.louzhu.common.utils.Query;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;

import com.louzhu.gulimall.product.dao.SkuInfoDao;
import com.louzhu.gulimall.product.entity.SkuInfoEntity;
import org.springframework.util.StringUtils;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Autowired
    private SkuImagesService skuImagesService;

    @Autowired
    private SpuInfoDescService spuInfoDescService;

    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private SkuSaleAttrValueService skuSaleAttrValueService;


    @Autowired
    private ThreadPoolExecutor executor;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkuInfo(SkuInfoEntity skuInfoEntity) {
        this.baseMapper.insert(skuInfoEntity);
    }

    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {
        // 查询条件器
        QueryWrapper<SkuInfoEntity> queryWrapper = new QueryWrapper<>();
        /*
        1-模糊查询关键字 key
         */
        String key = (String) params.get("key");
        if(!StringUtils.isEmpty(key)){
            queryWrapper.and((wrapper)->{
                wrapper.eq("sku_id",key).or().like("sku_name",key);
            });
        }

        /*
        2-三级分类ID
         */
        String catelogId = (String) params.get("catelogId");
        if(!StringUtils.isEmpty(catelogId)&&!"0".equalsIgnoreCase(catelogId)){

            queryWrapper.eq("catelog_id",catelogId);
        }

        /*
        3-查询条件品牌ID
         */
        String brandId = (String) params.get("brandId");
        if(!StringUtils.isEmpty(brandId)&&!"0".equalsIgnoreCase(catelogId)){
            queryWrapper.eq("brand_id",brandId);
        }

        /*
        4-查询条件价格最小值
         */
        String min = (String) params.get("min");


        if(!StringUtils.isEmpty(min)){
            try{
                BigDecimal bigDecimal = new BigDecimal(min);
                if(bigDecimal.compareTo(new BigDecimal("0"))==1){
                    queryWrapper.ge("price",min);
                }
            }catch (Exception e){

            }

        }

        /*
        5-查询条件：价格最大值
         */
        String max = (String) params.get("max");

        if(!StringUtils.isEmpty(max)  ){
            try{
                BigDecimal bigDecimal = new BigDecimal(max);

                if(bigDecimal.compareTo(new BigDecimal("0"))==1){
                    queryWrapper.le("price",max);
                }
            }catch (Exception e){

            }

        }


        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public List<SkuInfoEntity> getBySpuId(Long spuId) {
        List<SkuInfoEntity> skuInfoEntities = this.list(new QueryWrapper<SkuInfoEntity>().eq("spu_id", spuId));
        return skuInfoEntities;
    }

    /**
     * 获取sku详情
     * @param skuId
     * @return
     */
    @Override
    public SkuItemVo item(Long skuId) throws ExecutionException, InterruptedException {

        SkuItemVo skuItemVo = new SkuItemVo();

        /*
        1、sku的基本信息
         */
        CompletableFuture<SkuInfoEntity> skuInfoEntityFuture = CompletableFuture.supplyAsync(() -> {
            SkuInfoEntity skuInfoEntity = getById(skuId);
            skuItemVo.setInfo(skuInfoEntity);
            return skuInfoEntity;
        }, executor);

        /*
        3、sku 销售属性组合
         */
        CompletableFuture<Void> setSaleAttrFuture = skuInfoEntityFuture.thenAcceptAsync((result) -> {
            List<SkuItemSaleAttrVo> skuItemSaleAttrVos = skuSaleAttrValueService.getSaleAttrsBuSpuId(result.getSpuId());
            skuItemVo.setSaleAttr(skuItemSaleAttrVos);
        }, executor);


        /*
        4、sku 的介绍信息
         */

        CompletableFuture<Void> setDescFuture = skuInfoEntityFuture.thenAcceptAsync((result) -> {
            SpuInfoDescEntity spuInfoDescEntity = spuInfoDescService.getById(result.getSpuId());
            skuItemVo.setDesc(spuInfoDescEntity);
        }, executor);


        /*
        5、spu 的规格属性组合信息
         */
        CompletableFuture<Void> setGroupAttrsFuture = skuInfoEntityFuture.thenAcceptAsync((result) -> {
            List<SpuItemAttrGroupVo> spuItemAttrGroupVos = attrGroupService.getAttrGroupWithAttrsBySpuId(result.getSpuId(), result.getCatelogId());
            skuItemVo.setGroupAttrs(spuItemAttrGroupVos);
        }, executor);

        /*
        2、sku的图片信息
         */

        CompletableFuture<Void> setImagesFuture = CompletableFuture.runAsync(() -> {
            List<SkuImagesEntity> imagesEntities = skuImagesService.getImagesBySkuId(skuId);
            skuItemVo.setImages(imagesEntities);
        }, executor);

        /*
        等待所有任务完成
         */
        CompletableFuture.allOf(setSaleAttrFuture, setDescFuture, setGroupAttrsFuture, setImagesFuture).get();
        return skuItemVo;
    }

}